package com.bookyourtrek.model;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang3.StringUtils;

import com.bookyourtrek.util.DbConnection;
import com.mysql.jdbc.Statement;

public class ImageModel {

	private PreparedStatement ps = null;
	private ResultSet rs = null;
	private String sqlQuery = "";
	private Connection conn = null;
	
	/**
	 * function to insert images
	 * @author ananda
	 * @param trekid
	 * @param string
	 */
	public void insertimage(BigInteger trekid, String path) {
		
		sqlQuery = "INSERT INTO `bookyourtrek`.`trekimage`"
				+ " (`trekkingid`, `path`) VALUES (?,?);";
		conn = DbConnection.getConnection();
		try {
			ps = conn.prepareStatement(sqlQuery);
			ps.setLong(1, trekid.longValue());
			ps.setString(2, path);
			ps.executeUpdate();
			//get id of inserted record
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			
		}
	}

	public ArrayList<ImagePojo> getimages(BigInteger trekid) {
		ArrayList<ImagePojo> imgpojo = new ArrayList<ImagePojo>();
		sqlQuery = "SELECT imgid, trekkingid, path FROM bookyourtrek.trekimage"
				+ " where trekkingid = ?;";
		conn = DbConnection.getConnection();
		try {
			ps = conn.prepareStatement(sqlQuery);
			ps.setLong(1, trekid.longValue());
			rs = ps.executeQuery();
			while (rs.next()) {
				ImagePojo ip = new ImagePojo();
				ip.setImgid(BigInteger.valueOf(rs.getLong(1)));
				ip.setTrekkingid(BigInteger.valueOf(rs.getLong(2)));
				ip.setPath(rs.getString(3));
				imgpojo.add(ip);
			}		
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			
		}
		return imgpojo;
	}

}
